<template>
  <div class="app-container oneSearchNoFooter">
    <div class="customContainer ">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <dj-store-select :category="236" :defaultStore="formInline.store_id" @change="selectStoreChanged" />
        <el-form-item>
          <el-date-picker v-model="pickerDate" value-format="yyyy-MM-dd" type="daterange" align="right" unlink-panels range-separator="-"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
        <!-- <el-form-item>
          <el-tooltip class="item" effect="dark" placement="bottom-end">
            <div slot="content" style="line-height:20px;width:400px">
              剩余数量（个）:该门店的实际库存（主仓+中转仓+机台仓）
              <br> 金额（元）:该门店的实际库存价值的价格=礼品成本价*礼品数
            </div>
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-form-item> -->
      </el-form>
      <!-- 表格 -->
      <dj-simple-table :columns="tableColumns" :data="listData" :loading="listLoading" />
    </div>
  </div>
</template>
<script>
import { getList } from "@/api/report/store_stock_summary_form";
// import { getList } from "@/api/storage_control/storeCollect_form";

import {
  pickerOptionsF,
  pickerDateDot,
  optionMaxTime,
  getDateString
} from "@/utils";
import { DjSimpleTable } from "@/components/table";
import { DjStoreSelect } from "@/components/store";
export default {
  components: {
    "dj-simple-table": DjSimpleTable,
    "dj-store-select": DjStoreSelect
  },
  data() {
    return {
      tableColumns: [
        {
          label: "时间",
          dtcol: "time"
        },
        {
          label: "门店名称",
          dtcol: "store",
          dtkey: "store_name"
        },
        {
          label: "主仓库存金额",
          dtcol: "a",
          dtkey: "money",
          isnum: true,
          link: "report/store_stock_summary_detail"
        },
        {
          label: "礼品机中转仓库存金额",
          dtcol: "b",
          dtkey: "money",
          isnum: true,
          link: "report/store_stock_summary_detail"
        },
        {
          label: "摊位机中转仓库存金额",
          dtcol: "c",
          dtkey: "money",
          isnum: true,
          link: "report/store_stock_summary_detail"
        },
        {
          label: "礼品柜中转仓库存金额",
          dtcol: "d",
          dtkey: "money",
          isnum: true,
          link: "report/store_stock_summary_detail"
        },
        {
          label: "企划活动中转仓库存金额",
          dtcol: "e",
          dtkey: "money",
          isnum: true,
          link: "report/store_stock_summary_detail"
        },
        {
          label: "员工内购仓库存金额",
          dtcol: "f",
          dtkey: "money",
          isnum: true,
          link: "report/store_stock_summary_detail"
        },
        {
          label: "破损礼品中转仓库存金额",
          dtcol: "g",
          dtkey: "money",
          isnum: true,
          link: "report/store_stock_summary_detail"
        },
        {
          label: "机台仓库存金额",
          dtcol: "h",
          dtkey: "money",
          isnum: true,
          link: "report/store_stock_summary_detail"
        },
        {
          label: "合计",
          dtcol: "total"
        },
      ],
      listData: [],
      listLoading: false,
      formInline: {
        store_id: this.$store.getters.optionId,
        start_time: "", //2019-11-01
        end_time: "", //2019-11-30
        look_del: "0"
      },
      pickerOptions: pickerOptionsF(-1),
      pickerDate: pickerDateDot(-1,-1)
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      vm.beforeComingIn();
    });
  },
  methods: {
    beforeComingIn() {
      if (this.$route.query.routerflag) {
        // console.log(this.$route.query, "zst1119 beforeComingIn route.query");
        let col = this.$route.query.col;
        let row = this.$route.query.row;
        let time = row.time
        if(col == 'month_money') {
          time = row.time 
        } else { 
          let nowTime = new Date(time)
          var year = nowTime.getFullYear();
          var month = nowTime.getMonth();
          if(month == 0){
              month = 12;
              year = year - 1;
          }  
          time = year + '-' + (month > 0 ? '0'+ month : month)
        } 
        // 本月第一天至昨天
        let fist_day = getDateString('this_month_first_day', time);
        let date = new Date(time);
        let now = new Date();
        let yesterday;
        if (date.getMonth() == now.getMonth()) {
          yesterday = getDateString('yesterday');
        } else {
          yesterday = getDateString('this_month_last_day', time);
        }
        this.pickerDate = [fist_day,yesterday]
        this.formInline.store_id = this.$route.query.row.store_id;
        // this.searchClick(); //首次打开页面会请求两次
      } 
      // else {
      //   if (!this.listData || this.listData.length == 0) {
      //     this.searchClick();
      //   }
      // }
    },
    selectStoreChanged(obj) {
      // console.log(obj, "11111111111111111111111");
      for (let key in obj) this.formInline[key] = obj[key];
      this.searchClick();
    },
    // 搜索
    searchClick() {
      // console.log("222222222222222222");
      optionMaxTime(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.start_time = this.pickerDate ? this.pickerDate[0] : "";
        this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : "";
        this.fetchData();
      });
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      getList(this.formInline)
        .then(response => {
          // console.log(66,response);
          this.listData = [];
          if (response && response.message && response.message.length > 0) {
            response.message.forEach(ele => {
              let element = ele.row;
              element.forEach(val=>{
                val.money=(val.money*1).toFixed(2);
              })
              // console.log(34,element)
              if (element && element.length > 7) {
                this.listData.push({
                  time: ele.time,
                  store: {
                    store_id: ele.store_id,
                    store_name: ele.store_name
                  },
                  // 主仓库存
                  a: {
                    money: element[0].money,
                    house_id: element[0].house_id,
                    is_store_main: element[0].is_store_main
                  },
                  // 礼品机中转仓
                  b: {
                    money: element[1].money,
                    house_id: element[1].house_id,
                    is_store_main: element[1].is_store_main
                  },
                  // 摊位机中转仓
                  c: {
                    money: element[6].money,
                    house_id: element[6].house_id,
                    is_store_main: element[6].is_store_main
                  },
                  // 礼品柜中转仓
                  d: {
                    money: element[2].money,
                    house_id: element[2].house_id,
                    is_store_main: element[2].is_store_main
                  },
                  // 企划活动中转仓
                  e: {
                    money: element[4].money,
                    house_id: element[4].house_id,
                    is_store_main: element[4].is_store_main
                  },
                  // 员工内购仓
                  f: {
                    money: element[3].money,
                    house_id: element[3].house_id,
                    is_store_main: element[3].is_store_main
                  },
                  // 破损礼品中转仓
                  g: {
                    money: element[5].money,
                    house_id: element[5].house_id,
                    is_store_main: element[5].is_store_main
                  },
                  // 机台仓库存
                  h: {
                    money: element[7].money,
                    house_id: element[7].house_id,
                    area_id: element[7].area_id,
                    is_store_main: element[7].is_store_main
                  },
                  // 合计
                  total: (element[0].money*1+element[1].money*1+element[2].money*1+element[3].money*1+element[4].money*1+element[5].money*1+element[6].money*1+element[7].money*1).toFixed(2)
                });
              }
              // console.log(element, "zst getList");
            });
          }
          //   this.listData = response;
          // console.log(this.listData,3)
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
        });
    }
  }
};
</script>